import apis from "../services/applicationApi";

export default {
  namespace: "appListData",
  state: {
    appList: [],
    appDetail: {},
    totalCount: 0,
    loading: true
  },
  effects: {
    *getAppList({ payload }, { call, put }) {
      yield put({
        type: "setLoading",
        payload: true
      });
      const { datas, totalCount } = yield call(apis.getAppList, payload);
      yield put({
        type: "setState",
        payload: { appList: datas, totalCount }
      });
      yield put({
        type: "setLoading",
        payload: false
      });
    },
    *queryAppDetail({ payload }, { call, put }) {
      const { datas } = yield call(apis.queryAppDetail, payload);
      yield put({
        type: "setState",
        payload: { appDetail: datas }
      });
      return datas;
    },
    *addApp({ payload }, { call }) {
      return yield call(apis.addApp, payload);
    },
    *editApp({ payload }, { call }) {
      return yield call(apis.editApp, payload);
    },
    *deleteApp({ payload }, { call }) {
      return yield call(apis.deleteApp, payload);
    },
    *clearDetail(empty, { put }) {
      yield put({ type: "setState", payload: { appDetail: {} } });
    }
  },
  reducers: {
    setState: (state, { payload = {} }) => ({
      ...state,
      ...payload
    }),
    setLoading: (state, { payload }) => ({
      ...state,
      loading: payload
    })
  }
};
